package com.fmistuff.chain.nodes;

import java.util.HashMap;
import java.util.Map;

public class TermNode extends MarkovNode {

	public static final String HASHKEY = TermNode.class.getSimpleName();

	private static Map<String, MarkovNode> pool;
	private static final String MULTIPLICATION = "*";
	private static final String DIVISION = "/";

	static {
		pool = new HashMap<String, MarkovNode>();
		pool.put(MULTIPLICATION, new TermNode(MULTIPLICATION));
		pool.put(DIVISION, new TermNode(DIVISION));
		pool.put("factor", new FactorNode());

		pool.get(MULTIPLICATION).setProbability(0.1f);
		pool.get(DIVISION).setProbability(0.1f);
	}
	
	public TermNode() {
		this(pickANode(pool).getData());
	}

	protected TermNode(String data) {
		this(data, 0.5f);
	}

	protected TermNode(String data, float probability) {
		super(data, probability);
	}

	@Override
	public String descend() {
		//pick a node
		MarkovNode currentNode = pickANode(pool);
		
		//apply transformation 
		if (!isFinal()) {
			if (currentNode instanceof TermNode) {
				final String data = getData();
				return descend() + data + currentNode.descend(); //data = * or data = /
			}
			else if (currentNode instanceof FactorNode) { // if it is a factor node, enough!
				return currentNode.descend();
			}
		}

		//return it
		return getData();
	}
	
	@Override
	public boolean isFinal() {
		return false;
	}

}
